package com.ruoyi.project.channel.channelCode.controller;

import java.io.IOException;
import java.util.List;
import java.util.Random;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.channel.channelAccount.domain.ChannelAccount;
import com.ruoyi.project.channel.channelAccount.service.IChannelAccountService;
import com.ruoyi.project.pay.WebSocketServer;
import com.ruoyi.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.channel.channelCode.domain.ChannelCode;
import com.ruoyi.project.channel.channelCode.service.IChannelCodeService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 钉钉产码信息操作处理
 *
 * @author CAIN
 * @date 2019-06-21
 */
@Controller
@RequestMapping("/channel/channelCode")
public class ChannelCodeController extends BaseController {
    private String prefix = "channel/channelCode";

    @Autowired
    private IChannelCodeService channelCodeService;
    @Autowired
    private IChannelAccountService channelAccountService;

    @RequiresPermissions("channel:channelCode:view")
    @GetMapping()
    public String channelCode() {
        return prefix + "/channelCode";
    }

    /**
     * 查询钉钉产码列表
     */
    @RequiresPermissions("channel:channelCode:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ChannelCode channelCode) {
        startPage();
        List<ChannelCode> list = channelCodeService.selectChannelCodeList(channelCode);
        return getDataTable(list);
    }


    /**
     * 导出钉钉产码列表
     */
    @RequiresPermissions("channel:channelCode:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ChannelCode channelCode) {
        List<ChannelCode> list = channelCodeService.selectChannelCodeList(channelCode);
        ExcelUtil<ChannelCode> util = new ExcelUtil<ChannelCode>(ChannelCode.class);
        return util.exportExcel(list, "channelCode");
    }

    /**
     * 新增钉钉产码
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        ChannelAccount search = new ChannelAccount();
        // 钉钉群收款模式
        search.setPayType("08");
        // 取身份信息
        User user = getSysUser();
        String roleKey = ShiroUtils.getSysUser().getRoles().get(0).getRoleKey();
        if (roleKey.equals("channel")) {
            // 非admin 角色 只能查看下级商户信息
            search.setChannelUserId(user.getUserId());
        }
        List<ChannelAccount> accounts = channelAccountService.selectChannelAccountList(search);
        mmap.put("accountInfos", accounts);
        return prefix + "/add";
    }

    /**
     * 新增保存钉钉产码
     */
    @RequiresPermissions("channel:channelCode:add")
    @Log(title = "钉钉产码", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ChannelCode channelCode) {
        try {
            // "621401630"
            String s = "公司活动培训费聚餐旅游"; // 备注如果非中文会被风控 随机中文
            String result = "";
            Random ra = new Random();
            for (int i = 0; i < 9; i++) {
                result = result + s.charAt(ra.nextInt(s.length()));
            }
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("money", channelCode.getAmount() + "");
            jsonObject.put("mark", result);
            jsonObject.put("uid", channelCode.getMasterId());
            WebSocketServer.sendInfo(jsonObject.toJSONString(), channelCode.getMasterId());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return toAjax(1);
    }

    /**
     * 修改钉钉产码
     */
    @GetMapping("/edit/{ddId}")
    public String edit(@PathVariable("ddId") Long ddId, ModelMap mmap) {
        ChannelCode channelCode = channelCodeService.selectChannelCodeById(ddId);
        mmap.put("channelCode", channelCode);
        return prefix + "/edit";
    }

    /**
     * 修改保存钉钉产码
     */
    @RequiresPermissions("channel:channelCode:edit")
    @Log(title = "钉钉产码", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ChannelCode channelCode) {
        return toAjax(channelCodeService.updateChannelCode(channelCode));
    }

    /**
     * 删除钉钉产码
     */
    @RequiresPermissions("channel:channelCode:remove")
    @Log(title = "钉钉产码", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(channelCodeService.deleteChannelCodeByIds(ids));
    }

}
